﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScooterLand
{
    public partial class InvoiceForm : Form
    {
        private int orderID; // private variable

        Backend.Program controller = new Backend.Program(); // connect to backend code

        public InvoiceForm(int cid = 0)
        {
            InitializeComponent();
            orderID = cid; // set orderID equal to input paramenter
        }

        private void orderItem_Click(object sender, EventArgs e)
        {
            OrderForm oForm = new OrderForm();
            oForm.Show();
            this.Close();
        }

        private void customersItem_Click(object sender, EventArgs e)
        {
            CustomerForm cForm = new CustomerForm();
            cForm.Show();
            this.Close();
        }

        private void productsItem_Click(object sender, EventArgs e)
        {
            ProductForm pForm = new ProductForm();
            pForm.Show();
            this.Close();
        }

        private void employeesItem_Click(object sender, EventArgs e)
        {
            EmployeeForm eForm = new EmployeeForm();
            eForm.Show();
            this.Close();
        }

        private void invoicesItem_Click(object sender, EventArgs e)
        {
            InvoiceForm iForm = new InvoiceForm();
            iForm.Show();
            this.Close();
        }

        private void exitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InvoiceForm_Load(object sender, EventArgs e)
        {
            if (orderID != 0)
            {
                txtBoxInvoice.Text = controller.GetInvoice(orderID);
            }
            else
            {
                // loads employees
                DataTable employeeTable = controller.ListEmployees();

                cmbMechanic.DataSource = employeeTable;
                cmbMechanic.DisplayMember = "fullname";
                cmbMechanic.ValueMember = "eid";

                cmbMechanic.SelectedValue = 0;
                cmbMechanic.SelectedText = "-- Vælg mekaniker --";
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            lvInvoices.Items.Clear();
            txtBoxInvoice.Clear();

            int employeeID = Convert.ToInt32(cmbMechanic.SelectedValue);

            DataTable resultTable = controller.FindInvoices(txtTelephone.Text, txtFullName.Text, employeeID);
            DataRow[] resultRows = resultTable.Select();

            foreach (DataRow row in resultRows)
            {
                string[] customerInfo = controller.GetCustomerInfo(Convert.ToInt32(row[2]));
                string[] employeeInfo = controller.GetEmployeeInfo(Convert.ToInt32(row[3]));

                string customerName = customerInfo[1];
                string employeeName = employeeInfo[1];

                string[] str = { row[0].ToString(), row[1].ToString(), customerName, employeeName, row[4].ToString() };
                ListViewItem lvItem = new ListViewItem(str);
                lvInvoices.Items.Add(lvItem);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            // Set Ordre ID
            orderID = Convert.ToInt32(lvInvoices.SelectedItems[0].SubItems[0].Text);
            txtBoxInvoice.Text = controller.GetInvoice(orderID);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            string[] strArray = controller.GetInvoice(orderID).Split('\n');
            string result = controller.PrintInvoice(orderID, strArray);

            if (result != "")
                MessageBox.Show("Udskrevet som " + result);
        }
    }
}
